import RPi.GPIO as GPIO
import gui

gar0_sens=14
gar1_sens=15
gar2_sens=18
gar3_sens=23

input_sense=25
buzzer=16



def setup():
    GPIO.setmode(GPIO.BCM)
    # GPIO.setup(input_channels,GPIO.IN)
    GPIO.setup(gar0_sens,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(gar1_sens,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(gar2_sens,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(gar3_sens,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(input_sense,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

    GPIO.setup(buzzer,GPIO.OUT)
    GPIO.output(buzzer,True)

def buzzer_start():
    GPIO.output(buzzer,False)

def buzzer_stop():
    GPIO.output(buzzer,True)

def scan_sensors():
    sens_num=0
    if GPIO.input(gar0_sens)==1:
        gui.window['gar1_status'].update(button_color=('green','red'))
        
        sens_num+=1
    else:
        gui.window['gar1_status'].update(button_color=('red','green'))
    if GPIO.input(gar1_sens)==1:
        gui.window['gar2_status'].update(button_color=('green','red'))
        
        sens_num+=1
    else:
        gui.window['gar2_status'].update(button_color=('red','green'))  
    if GPIO.input(gar2_sens)==1:
        gui.window['gar3_status'].update(button_color=('green','red'))
        
        sens_num+=1
    else:
        gui.window['gar3_status'].update(button_color=('red','green'))   
    if GPIO.input(gar3_sens)==1:
        gui.window['gar4_status'].update(button_color=('green','red'))
        
        sens_num+=1
    else:
        gui.window['gar4_status'].update(button_color=('red','green'))    
    if GPIO.input(input_sense)==1:
        sens_num+=1
        
    if sens_num>0:
        buzzer_start()
    else:
        buzzer_stop()
        gui.alarm_sign_stop()
        

# def alarm():
#     sens=scan_sensors()
#     if sens<5:
#         buzzer_start()
#         # gui.window['gar'+ str(sens) +'_status'].update(button_color=('green','red'))
#     else:
#         buzzer_stop()
#         # gui.alarm_sign_stop()
#     return sens

def destroy():
    GPIO.cleanup()

# if __name__ == '__main__':
#     setup()
#     while True:
#         try:
#             sens=scan_sensors()
#             if sens<5:
#                 buzzer_start()
#                 print(sens)
#             else:
#                 buzzer_stop()
#         except KeyboardInterrupt:
#             destroy()